﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Sharpie.Libs;
using Microsoft.Xna.Framework;

namespace Sharpie
{
    public class Map
    {
        private List<Point> schem;
        private List<string> rawschem;

        public List<Point> Schematic
        {
            get
            {
                if (schem == null)
                {
                    schem = new List<Point>();
                    foreach (string s in rawschem)
                    {
                        schem.Add(new Point(
                            Convert.ToInt32(s.Split(',')[0]),
                            Convert.ToInt32(s.Split(',')[1])));
                    }
                }
                return schem;
            }
            set
            {
                schem = value;
            }
        }
        public string Author { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public List<Point> Startpoints { get; set; }
        public Size BoardSize { get; set; }

        public Map(Stream mapStream)
        {
            using (mapStream)
            {
                using (StreamReader sr = new StreamReader(mapStream))
                {
                    sr.ReadLine();
                    Title = Encryption.Decrypt(sr.ReadLine(), Encryption.SharpieKey);
                    Author = Encryption.Decrypt(sr.ReadLine(), Encryption.SharpieKey);
                    Description = Encryption.Decrypt(sr.ReadLine(), Encryption.SharpieKey);
                    string[] startpoints = Encryption.Decrypt(sr.ReadLine(), Encryption.SharpieKey).Split(';');
                    Startpoints = new List<Point>();
                    foreach (string s in startpoints)
                    {
                        string[] point = s.Split(',');
                        Startpoints.Add(new Point(Convert.ToInt32(point[0]), Convert.ToInt32(point[1])));
                    }
                    string[] boardsize = Encryption.Decrypt(sr.ReadLine(), Encryption.SharpieKey).Split('x');
                    BoardSize = new Size(Convert.ToInt32(boardsize[0]), Convert.ToInt32(boardsize[1]));
                    rawschem = new List<string>();
                    while (!sr.EndOfStream)
                    {
                        rawschem.Add(Encryption.Decrypt(sr.ReadLine(), Encryption.SharpieKey));
                    }

                }
            }
        }

        public class Size
        {
            public int Width, Height;

            public Size(int width, int height)
            {
                Width = width;
                Height = height;
            }

            public Size()
                : this(0, 0) { }
        }
    }
}
